package springboot.study.project.util.threadpool;


import springboot.study.project.util.common.Util;
import springboot.study.project.util.db.BaseDao;
import springboot.study.project.util.db.FormatDataUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    private static Map<Integer, Map<String, Object>> sourcrData = new HashMap<Integer, Map<String, Object>>();

    public static Map<String, Object> getData(int key) {
        Map<String, Object> data1 = new HashMap<String, Object>();
        data1.put("id", "1");
        data1.put("name", "name_1");
        data1.put("age", "22");
        data1.put("birthday", "2020-10-01 15:35:50");
        Map<String, Object> data2 = new HashMap<String, Object>();
        data2.put("id", "1");
        data2.put("name", "name_2");
        data2.put("age", "22");
        data2.put("birthday", "2020-10-01 15:35:50");
        Map<String, Object> data3 = new HashMap<String, Object>();
        data3.put("id", "1");
        data3.put("name", "name_3");
        data3.put("age", "22");
        data3.put("birthday", "2020-10-01");
        Map<String, Object> data4 = new HashMap<String, Object>();
        data4.put("id", "1");
        data4.put("name", "name_4");
        data4.put("age", "22");
        data4.put("birthday", "2020-10-01 15:35:50");

        Map<String, Object> sourceData = new HashMap<String, Object>();
        List<Map<String, Object>> sourceDataList = new ArrayList<Map<String, Object>>();

        switch (key) {
            case 1:
                for (int i = 0; i < 10000; i++) {
                    sourceDataList.add(data1);
                }
                break;
            case 2:
                for (int i = 0; i < 50000; i++) {
                    sourceDataList.add(data2);
                }
                break;
            case 3:
                for (int i = 0; i < 100000; i++) {
                    sourceDataList.add(data3);
                }
                break;
            case 4:
                for (int i = 0; i < 200000; i++) {
                    sourceDataList.add(data4);
                }
                break;
            default:
                break;
        }
        sourceData.put("tableName", "student");
        sourceData.put("data", sourceDataList);

        return sourceData;
    }

    static {
        for (int i = 1; i <= 4; i++) {
            sourcrData.put(i, getData(i));
        }
    }

    public static void main(String[] args) {
        ExecutorProcessPool pool = ExecutorProcessPool.getInstance();

        for (int i = 1; i <= 4; i++) {
            pool.execute(new ExcuteTask(i));
        }

        // 关闭线程池，如果是需要长期运行的线程池，不用调用该方法。
        // 监听程序退出的时候最好执行一下。
        pool.shutdown();
    }

    /**
     * 执行任务2，实现Runable方式
     */
    static class ExcuteTask implements Runnable {
        private int taskName;

        public ExcuteTask(int taskName) {
            this.taskName = taskName;
        }

        @Override
        public void run() {
            try {
                System.out.println("准备接受数据.....");
                long starTime = System.currentTimeMillis();

                // 接收数据，解析数据
                Map<String, Object> sourceData = sourcrData.get(taskName);
                String tableName = Util.getStrOfObj(sourceData.get("tableName")); // 表名称
                List<Map<String, Object>> data = Util.getListOfObj(sourceData.get("data")); // 数据map
                if (data.size() <= 0) {
                    System.out.println("未接收到数据，请检查请求是否成功！");
                    return;
                }

                // 数据类型转换
                List<Map<String, Object>> dataList = FormatDataUtil.formatData(tableName, data);

                // 执行sql,保存数据
                String sql = Util.getSql(tableName, dataList.get(0));
                System.out.println("待执行Sql：" + sql);

                List<List<Object>> valuesList = Util.getBatchInsertData(dataList, dataList.get(0).size());
                System.out.println(BaseDao.batchExecuteSqlByPre(sql, valuesList));

                long endTime = System.currentTimeMillis();
                long Time = endTime - starTime;
                System.out.println("完成数据插入....." + Time / 1000.0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("-------------这里执行业务逻辑，Runnable TaskName = " + taskName + "-------------");
        }
    }
}
